package com.stu.ssm_02.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.ssm_02.entity.User;
import com.stu.ssm_02.service.IUserService;
import com.stu.ssm_02.vo.AjaxResult;
import com.stu.ssm_02.vo.AjaxTableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ClassName: UserController
 * Package: com.stu.ssm_02.controller
 * Description: @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/list")
    public String getList(Model model){
//        PageHelper.startPage(1,10);
        List<User> userList=userService.getList();
//        PageInfo<User> of = PageInfo.of(userList);
        model.addAttribute("userList",userList);
        return "user/list";
    }
    @RequestMapping("/goAjaxList")
    public String goToListAjax(){
        return "user/listAjax";
    }

    @RequestMapping("/listAjax")
    @ResponseBody
    public AjaxTableResult<User> getListEntity(Model model){
        List<User> userList=userService.getList();
        AjaxTableResult<User> result=new AjaxTableResult<>();
        result.setRows(userList);
        //自己查询数据库中的所有数据总和
        result.setTotal(userList.size());
        return result;
    }
    @RequestMapping("/detail")
    public String goToDetail(Integer id,Model model){
        User user=userService.getUserById(id);
        model.addAttribute("user",user);
        return "user/detail";
    }
    @PostMapping("/upload")
//    变量名必须与前端传递的key值保持一致
    public String upLoadPic(MultipartFile file,Model model){
        System.out.println(file);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date());
        String imgFile=format+file.getOriginalFilename();
        System.out.println(imgFile);
        //上传图片
//        这一步只是创建了一个空壳文件，就是有这个文件但是里面没有内容
        File f=new File("E:\\ideaWorkSpace\\SSM_02\\src\\main\\resources\\upload\\"+imgFile);
        try {
            //将file中的内容传给f
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        model.addAttribute("imgPic",imgFile);
        return "user/pic";
    }
    @PostMapping("/ajaxUpload")
    @ResponseBody
//    变量名必须与前端传递的key值保持一致
    public String uploadAjax(MultipartFile file,Integer id){
        System.out.println(file);
        System.out.println("id="+id);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date());
        String imgFile=format+file.getOriginalFilename();
        System.out.println(imgFile);
        //上传图片
//        这一步只是创建了一个空壳文件，就是有这个文件但是里面没有内容
//        File f=new File("E:\\ideaWorkSpace\\SSM_02\\src\\main\\resources\\upload\\"+imgFile);
        File f=new File("E:\\ideaWorkSpace\\SSM_02\\src\\main\\resources\\upload\\"+file.getOriginalFilename());
        try {
            //将file中的内容传给f
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //数据库保存
        User user=new User();
        user.setId(id);
        user.setImage(file.getOriginalFilename());
        int result=userService.updateImgById(user);
        return file.getOriginalFilename();
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public AjaxResult<Integer> deleteUserById(int id){
        AjaxResult<Integer> ajaxResult=new AjaxResult<>();
        Integer result = userService.deleteUserById(id);
        if (result>0){
            ajaxResult.setMsg("删除成功");
        }else {
            ajaxResult.setCode(400);
            ajaxResult.setMsg("删除失败");
        }
        ajaxResult.setData(result);
        return ajaxResult;
    }

    @RequestMapping("/deleteUserList")
    @ResponseBody
    public AjaxResult<Integer> deleteUserById(@RequestBody List<Integer> userList){
        AjaxResult<Integer> ajaxResult=new AjaxResult<>();
        Integer result = userService.deleteUserList(userList);
        System.out.println(userList);
        if (result>0){
            ajaxResult.setMsg("删除成功");
        }else {
            ajaxResult.setCode(400);
            ajaxResult.setMsg("删除失败");
        }
        ajaxResult.setData(result);
        return ajaxResult;
    }
    @RequestMapping("/deleteUserListAjax")
    @ResponseBody
    public AjaxResult<Integer> deleteUserListById(@RequestBody List<User> userList){
        AjaxResult<Integer> ajaxResult=new AjaxResult<>();
        Integer result=userService.deleteUserListAjax(userList);
        System.out.println(userList);
        if (result>0){
            ajaxResult.setMsg("删除成功");
        }else {
            ajaxResult.setCode(400);
            ajaxResult.setMsg("删除失败");
        }
        ajaxResult.setData(result);
        return ajaxResult;
    }


    @RequestMapping("/getUserList")
    @ResponseBody
    public AjaxTableResult<User> getUserListAjax(Integer pageNum,Integer pageSize){
        AjaxTableResult<User> ajaxTableResult=new AjaxTableResult<>();
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList=userService.getUserListAjax();
        PageInfo<User> of = PageInfo.of(userList);
        System.out.println(of);
        ajaxTableResult.setRows(userList);
        ajaxTableResult.setTotal((int) of.getTotal());
        return ajaxTableResult;
    }

}
